package priotityqeue;

import java.util.Arrays;

public class MypriorityQueue {
    private int[] arr;
    private int size;

    public MypriorityQueue() {
        arr = new int[10];
    }
    public void offer(int val){
        //先把新元素尾插到数组末尾
        arr[size]=val;
        size++;
        //尾插之后，然后进行想上调用
        Heap.shiftUp(arr,size-1);
    }
    public Integer poll() {
        if (size == 0) {
            return null;
        }
        //把堆顶元素和最后一个元素交换，然后把最后一个元素删除
        int result = arr[0];
        arr[0]=arr[size-1];
        size--;
        Heap.shiftDown(arr,size,0);
        return result;
    }
    public Integer peek() {
        if (size == 0) {
            return null;
        }
        return arr[0];
    }

    @Override
    public String toString() {
        return "MypriorityQueue{" +
                "arr=" + Arrays.toString(arr) +
                ", size=" + size +
                '}';
    }

    public static void main(String[] args) {
        MypriorityQueue queue = new MypriorityQueue();
        queue.offer(5);
        queue.offer(3);
        queue.offer(7);
        queue.offer(1);
        queue.offer(4);
        System.out.println(queue);
        System.out.println(queue.poll());
        System.out.println(queue.peek());
        System.out.println(queue);

    }
}
